package com.arrayExamples;


/**
 * Created by yijy on 2017/8/18.
 * 随便输入一个数组，截取其中紧邻的元素作为子数组(长度最小为1)，并使得子数组中求和值最大
 */
public class MaxSumSubArray {
    public static void main(String[] args) {
        int[] arrs = {3,1,-3,4,-1,2,1,-5,4};
        int max =maxSubArray(arrs);
        System.out.println(max);
    }

    //3,1,-3,4,-1,2,1,-5,4
    public static int maxSubArray(int[] A) {
        int max = Integer.MIN_VALUE, sum = 0;
        System.out.println("min:"+max);
        for (int i = 0; i < A.length; i++) {
            if (sum < 0)
                sum = A[i];
            else
                sum += A[i];
            if (sum > max){
                System.out.println("数组中满足的子串序列号："+i);
                max = sum;
            }
        }
        return max;
    }

}
